﻿using System;
using System.Collections.Generic;
using System.Data;

namespace Sandy.Selectors
{
    // Path: client.SuperFamily("name")[keys]["superCol"]
    public class SubColumnRangeSelector
    {
        private readonly SandyClient _client;
        private readonly SandyFamily _family;
        private readonly KeyRange _keyRange;
        private readonly IEnumerable<string> _keys;
        private readonly string _superCol;

        internal SubColumnRangeSelector(SandyClient client, SandyFamily family, KeyRange keyRange, string superCol)
        {
            _client = client;
            _family = family;
            _keyRange = keyRange;
            _superCol = superCol;

            if (keyRange == null) throw new ArgumentException("KeyRange cannot be null");
        }

        internal SubColumnRangeSelector(SandyClient client, SandyFamily family, IEnumerable<string> keys, string superCol)
        {
            _client = client;
            _family = family;
            _keys = keys;
            _superCol = superCol;

            if (keys == null) throw new ArgumentException("Keys cannot be null");
        }

        public IDictionary<string, IDictionary<string, string>> AllColumns
        {
            get
            {
                // throw new NotImplementedException("request 36: get_range_slices(cf={0}, keyRange={1}, scol={2}");
                if (_keyRange != null)
                    return GetRangeSlicesQuery.PerformGetAll(_client, _family, _keyRange, _superCol);

                return MultigetSliceQuery.PerformGetAll(_client, _family, _keys, _superCol);
            }
        }

        public IDictionary<string, string> this[string column]
        {
            get
            {
                // throw new NotImplementedException("request 35: get_range_slices(cf={0}, keyRange={1}, scol={2}, col={3}");
                if (_keyRange != null)
                    return GetRangeSlicesQuery.Perform(_client, _family, _keyRange, _superCol, column);

                return MultigetSliceQuery.Perform(_client, _family, _keys, _superCol, column);
            }
        }

        public IDictionary<string, IDictionary<string, string>> this[IEnumerable<string> cols]
        {
            get
            {
                // throw new NotImplementedException("request 31: get_range_slices(cf={0}, keyRange={1}, scol={2}, cols={3}");
                if (_keyRange != null) 
                    return GetRangeSlicesQuery.Perform(_client, _family, _keyRange, _superCol, cols);

                return MultigetSliceQuery.Perform(_client, _family, _keys, _superCol, cols);
            }
        }

        public IDictionary<string, IDictionary<string, string>> this[params string[] cols]
        {
            get
            {
                // Request 32
                return this[(IEnumerable<string>)cols];
            }
        }

        public IDictionary<string, IDictionary<string, string>> this[Range colRange]
        {
            get
            {
                // throw new NotImplementedException("request 33: get_range_slices(cf={0}, keyRange={1}, scol={2}, colRange={3}");
                if (_keyRange != null)
                    return GetRangeSlicesQuery.Perform(_client, _family, _keyRange, _superCol, colRange);

                return MultigetSliceQuery.Perform(_client, _family, _keys, _superCol, colRange);
            }
        }
    }
}